/*
 * Instruction3A.java
 *
 * Created on March 21, 2007, 10:43 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
/*
Double accumulator D is loaded from the address indicated by the stack
pointer. The SP is then incremented by two.
The order in which A and B are pulled from the stack is the opposite of
the order in which A and B are pulled when an RTI instruction is
executed. The interrupt stacking order for A and B is
backward-compatible with the M6800, which had no 16-bit accumulator.
Pull instructions are commonly used at the end of a subroutine to restore
the contents of CPU registers that were pushed onto the stack before
subroutine execution.
 */
package moto9s12sim;

/**
 *
 * @author Fergus Earnshaw
 */
public class Instruction3A extends Instruction{
    
    /** Creates a new instance of Instruction3A */
    public Instruction3A() {
        this.strAssemblyCode = "PULD";
        this.bCodes = new byte[1];
        this.bCodes[0] = (byte)Integer.parseInt("3A",16);
    }
    
    public boolean excute(M9S12 cpu)
    {
       this.shAddr = cpu.PC.getRegister();
        cpu.PC.inc();
        byte bPageH, bPageL;
        
        //(M(SP) : M(SP+1)) => A : B
        //(SP) + $0002 => SP
        
        bPageH = cpu.bMemory[cpu.SP.getUnsignedRegister()];
        cpu.SP.inc();
        bPageL = cpu.bMemory[cpu.SP.getUnsignedRegister()];
        cpu.SP.inc();
        cpu.A.setRegister(bPageH);
        cpu.B.setRegister(bPageL);
        cpu.PC.inc();
        
        printInstruction(cpu);
        //I think the first invalid instruction should halt excution
        return true;
    }  
    
}
